package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"resr_fun-api/basic/config"
	__ "resr_fun-api/basic/proto"
	"resr_fun-api/handler/model"
)

func AddVideo(c *gin.Context) {
	var form model.AddVideoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.IQiYiClient.AddVideo(c, &__.AddVideoReq{
		Title:     form.Title,
		Info:      form.Info,
		CateTag:   form.CateTag,
		VipStatus: int64(form.VipStatus),
		Image:     form.Image,
		VideoUrl:  form.VideoUrl,
		Status:    int64(form.Status),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发布失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布成功",
		"data": video,
	})

}

func ProtobufVideo(c *gin.Context) {
	var form model.ProtobufForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	protobuf, err := config.IQiYiClient.Protobuf(c, &__.ProtobufReq{
		VideoId: int64(form.VideoId),
		Status:  int64(form.Status),
		Comment: form.Comment,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "审核提交失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "审核提交成功",
		"data": protobuf,
	})
}

func KeywordVideo(c *gin.Context) {
	var form model.KeywordVideoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.IQiYiClient.KeywordVideo(c, &__.KeywordVideoReq{Keyword: form.Keyword})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "筛选失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "筛选成功",
		"data": video,
	})
}

func InfoVideo(c *gin.Context) {
	var form model.InfoVideoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.IQiYiClient.InfoVideo(c, &__.InfoVideoReq{VideoId: int64(form.VideoId)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "加载失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查看成功",
		"data": video,
	})
}
